﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Tesla.Service.DataContracts;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Tesla.Util.Utilities;

namespace Tesla.Web.Pages
{
    public enum ERowType
    {
        TaskRow = 0,
        PhaseRow,
        ProjectRow
    }

    public partial class TSEntry : PageBase
    {
        #region Constants
        private const int TOTAL_GRIDCOLUMN = 13;
        private const int DAYCOLUMNSTARTINGINDEX = 5;

        private const int PROJECTID = 0;
        private const int PROJECTNAME = 1;
        private const int PHASEID = 2;
        private const int PHASENAME = 3;
        private const int TASKID = 4;
        private const int TASKNAME = 5;
        private const int DAY1 = 6;
        private const int DAY2 = 7;
        private const int DAY3 = 8;
        private const int DAY4 = 9;
        private const int DAY5 = 10;
        private const int DAY6 = 11;
        private const int DAY7 = 12;

        TimeSheetServiceDispatcher tsdisp = new TimeSheetServiceDispatcher();

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {

            DataSet dsTimeSheet = new DataSet();


            dsTimeSheet.Tables.AddRange(new DataTable[] 
                                            { DataTypeConversion.ConvertToDataTable(tsdisp.ReadTasksForTimeSheet(1, 2011, 1).Cast<TimeSheetDC>().ToList()) ,
                                                DataTypeConversion.ConvertToDataTable(tsdisp.ReadWeeksCalendar(1, 2011,DateTime.Now).Cast<TimesheetDaysDC>().ToList())} ); 

            if (!IsPostBack)
            {
                GridView1.DataBinding += new EventHandler(GridView1_DataBinding);
                GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);

                GridView1.DataSource = GridData(dsTimeSheet);
                GridView1.DataBind();
            }


            tsdisp.ReadTasksForTimeSheet(1, 2011, 1);

        }

        void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            GridViewRow gr = e.Row;
            DataRow dr = null;
            if (gr.RowType == DataControlRowType.DataRow)
            {
                dr = ((DataTable)GridView1.DataSource).Rows[gr.RowIndex];

                if (dr["TaskID"].ToString() == string.Empty &&
                    dr["PhaseID"].ToString() != string.Empty)
                {
                    //That means the row is a phase
                    ChangeTextBox(ERowType.PhaseRow, gr, dr);
                }
                else if (dr["TaskID"].ToString() != string.Empty)
                {
                    //Task row
                    ChangeTextBox(ERowType.TaskRow, gr, dr);
                }
                else
                {
                    //project row
                    ChangeTextBox(ERowType.ProjectRow, gr, dr);
                }
            }
        }

        private void ChangeTextBox(ERowType rtype, GridViewRow gr, DataRow dr)
        {
            for (int day = 1; day <= 7; day++)
            {
                if (gr.Cells[DAYCOLUMNSTARTINGINDEX + day].HasControls())
                {
                    TextBox txt = gr.Cells[DAYCOLUMNSTARTINGINDEX + day].FindControl("txtDay" + day.ToString()) as TextBox;
                    switch (rtype)
                    {
                        case ERowType.TaskRow:
                            txt.Attributes.Add("onchange", "sumField(this)");
                            txt.ID = txt.ID + "~" + dr["PhaseID"].ToString();
                            break;
                        case ERowType.PhaseRow:
                            txt.ID = txt.ID + "~Phase~" + dr["PhaseID"].ToString();
                            txt.ReadOnly = true;
                            gr.CssClass = "phase-row";
                            break;
                        case ERowType.ProjectRow:
                            gr.Cells[DAYCOLUMNSTARTINGINDEX + day].Controls.Clear();
                            gr.CssClass = "project-row";
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        void GridView1_DataBinding(object sender, EventArgs e)
        {
            DataTable dt = ((DataTable)GridView1.DataSource);
            for (int i = 7; i > 0; i--)
            {
                GridView1.Columns[TOTAL_GRIDCOLUMN - i].HeaderText = dt.Columns[dt.Columns.Count - i].ColumnName;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            //DataTable data = GridView1.DataSource as DataTable;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow gr = GridView1.Rows[i];
                for (int day = 1; day <= 7; day++)
                {
                    if (gr.Cells[DAYCOLUMNSTARTINGINDEX + day].HasControls())
                    {
                        TextBox txt = gr.Cells[DAYCOLUMNSTARTINGINDEX + day].FindControl("txtDay" + day.ToString()) as TextBox;
                        //TODO: Create time sheet object here
                    }
                }
            }
        }

        private DataTable GridData(DataSet ds)
        {
            DataTable dtProj = ds.Tables[0];
            DataTable dtWeek = ds.Tables[1];

            foreach (DataRow r in dtWeek.Rows)
            {
                DataColumn dc = new DataColumn();
                dc.ColumnName = r["calendardate"].ToString();
                dtProj.Columns.Add(dc);
            }
            return dtProj;
        }
    }
}